<?php

/**
 * DTOAN
 */
class SettingForm extends CFormModel {

    //First email using send when customer book from front end
    public $transportType; //php or smtp
    public $smtpHost;
    public $smtpUsername;
    public $smtpPassword;
    public $smtpPort;
    public $encryption;
    //Second Email using send when admin user config on backend send to customer sothat confirm book information
//    public $transportType1; //php or smtp
    public $smtpHost1;
    public $smtpUsername1;
    public $smtpPassword1;
    public $smtpPort1;
    public $encryption1;
    //general
    public $adminEmail;
    public $autoEmail;
    public $facebook;
    public $google;
    public $twitter;
    public $logo;
    public $link_youtube;
    public $image_watermark;
    public $title;
    public $meta_description;
    public $meta_keywords;    
    public $hot_line_vi;
    //public $hot_line_en;
    public $slogan_vi;
    //public $slogan_en;
    public $background;
    //public $comment_facebook;
    public $address;
    public $phone;
    public $email;
    public $tax_code;
    public $fax;
    public $yahoo;
    public $skype;
    public $company;

    public function rules() {
        $return = array();
        foreach (SettingForm::$arrGeneral as $key => $value):
            $return[] = array($value, 'safe');
        endforeach;
        foreach (SettingForm::$arrSmtp as $key => $value):
            $return[] = array($value, 'safe');
        endforeach;
        foreach (SettingForm::$arrSmtp1 as $key => $value):
            $return[] = array($value, 'safe');
        endforeach;
        $return [] = array('smtpPort', 'numerical', 'integerOnly' => true);
        $return [] = array('facebook', 'length', 'max' => 200);
        $return [] = array('image_watermark', 'file', 'on' => 'updateSettings',
            'allowEmpty' => true,
            'types' => 'jpg,gif,png,tiff',
            'wrongType' => 'Only jpg,gif,png,tiff allowed',
            'maxSize' => 1024 * 1024 * 3, // 8MB
            'tooLarge' => 'The file was larger than 3MB. Please upload a smaller file.',
        );
        $return [] = array('image_watermark', 'match', 'pattern' => '/^[^\\/?*:&;{}\\\\]+\\.[^\\/?*:&;{}\\\\]{3}$/', 'message' => 'Image files name cannot include special characters: &%$#', 'on' => 'updateSettings');
        /*
        $return [] = array('background', 'file', 'on' => 'updateSettings',
            'allowEmpty' => true,
            'types' => 'jpg,gif,png,tiff',
            'wrongType' => 'Only jpg,gif,png,tiff allowed',
            'maxSize' => 1024 * 1024 * 3, // 8MB
            'tooLarge' => 'The file was larger than 3MB. Please upload a smaller file.',
        );
        $return [] = array('background', 'match', 'pattern' => '/^[^\\/?*:&;{}\\\\]+\\.[^\\/?*:&;{}\\\\]{3}$/', 'message' => 'Image files name cannot include special characters: &%$#', 'on' => 'updateSettings');
        */
        $return [] = array('logo', 'file', 'on' => 'updateSettings',
            'allowEmpty' => true,
            'types' => 'jpg,gif,png,tiff',
            'wrongType' => 'Only jpg,gif,png,tiff allowed',
            'maxSize' => 1024 * 1024 * 3, // 8MB
            'tooLarge' => 'The file was larger than 3MB. Please upload a smaller file.',
        );
        $return [] = array('logo', 'match', 'pattern' => '/^[^\\/?*:&;{}\\\\]+\\.[^\\/?*:&;{}\\\\]{3}$/', 'message' => 'Image files name cannot include special characters: &%$#', 'on' => 'updateSettings');
        $return [] = array('transportType, smtpHost, smtpUsername, smtpPassword, encryption', 'length', 'max' => 100);
        $return [] = array('link_youtube, address, phone, email', 'safe');
        return $return;
    }

    public static $arrSmtp = array(
        'host' => 'smtpHost',
        'username' => 'smtpUsername', 
        'password' => 'smtpPassword',
        'port' => 'smtpPort',
        'encryption' => 'encryption',

    );
    public static $arrSmtp1 = array(
        'host' => 'smtpHost1',
        'username' => 'smtpUsername1',
        'password' => 'smtpPassword1',
        'port' => 'smtpPort1',
        'encryption' => 'encryption1'
    );
    public static $arrGeneral = array(
        'autoEmail' => 'autoEmail',
        'facebook' => 'facebook',
        'google' => 'google',
        'logo' => 'logo',
        'twitter'=>'twitter',
        'image_watermark' => 'image_watermark',
        'link_youtube'=>'link_youtube',
        'title' => 'title',
        'meta_description' => 'meta_description',
        'meta_keywords' => 'meta_keywords',       
        'hot_line_vi' => 'hot_line_vi',
        //'hot_line_en' => 'hot_line_en',
        'slogan_vi' => 'slogan_vi',
        'phone' => 'phone',
        'fax' => 'fax',
        'address'=>'address',
        'tax_code' =>'tax_code',
        'email' => 'email',
        'yahoo' => 'yahoo',
        'skype' => 'skype',
        'company' => 'company'
        //'slo gan_en' => 'slogan_en',
        //'background' => 'background',
        //'comment_facebook' => 'comment_facebook',
        
        
    );

    /**
     * Override configurations.
     */
    static public function applySettings() {
        //apply setting for paypal
        if (Yii::app()->setting->getItem('transportType')) {
            Yii::app()->mail->transportType = Yii::app()->setting->getItem('transportType');
//            Yii::app()->mail->transportType = Yii::app()->setting->getItem('transportType1');
        }
        
        if (Yii::app()->mail->transportType == 'smtp') {
            foreach (self::$arrSmtp as $key => $value) {
                if (Yii::app()->setting->getItem($value)) {
                    Yii::app()->mail->transportOptions[$key] = Yii::app()->setting->getItem($value);
                }
            }
        } else {
            Yii::app()->mail->transportOptions = '';
        }

        //apply setting for general
        foreach (self::$arrGeneral as $key => $value) {
            if (Yii::app()->setting->getItem($value)) {
                Yii::app()->params[$key] = Yii::app()->setting->getItem($value);
            }
        }

    }

}
